package cn.antalk.top.user.service.Impl;

import cn.antalk.top.user.dao.UserDao;
import cn.antalk.top.user.entity.User;
import cn.antalk.top.user.entity.UserLikeTalk;
import cn.antalk.top.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: WackyBoy
 * @Date: 2021/12/6 20:11
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User queryUserByAll(String uname, String password) {
        // 哈希加密密码
        User user = userDao.queryUserByUname(uname);
        if(user == null) return null;
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean match = passwordEncoder.matches(password, user.getPassword());
        if(match) {
            return user;
        } else return null;
    }

    @Override
    public Boolean register(String uname, String password) {
        // 加密密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String pwd_encoder = encoder.encode(password);
        // 插入成功返回1
        return userDao.registerUserByAll(uname, pwd_encoder) == 1;
    }

    @Override
    public UserLikeTalk queryUserForLogin(String uname, String password) {
        // 哈希加密密码
        User user = queryUserByAll(uname, password);
        if(user == null) return null;
        UserLikeTalk userLikeTalk = new UserLikeTalk();
        userLikeTalk.setUid(user.getUid());
        userLikeTalk.setAvatar(user.getAvatar());
        userLikeTalk.setUname(user.getUname());
        List<Integer> likeTalkid = this.userDao.queryAllLikeTalkIdByUid(user.getUid());
        userLikeTalk.setLikeTalkid(likeTalkid);
        return userLikeTalk;
    }


}
